package com.argrace.smart.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.argrace.smart.R;
import com.argrace.smart.adapter.QingJingAdapter;
import com.argrace.smart.base.BaseActivity;
import com.argrace.smart.base.ConstantConfigure;
import com.argrace.smart.model.LocalDb_QingJingTable;
import com.argrace.smart.ui.EmptyRecycleView;
import com.argrace.smart.util.Logg;
import com.chad.library.adapter.base.BaseQuickAdapter;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018\2\20 0020.
 */

public class QingJingListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {
    EmptyRecycleView recyclerView;
    SwipeRefreshLayout refreshLayout;
    private List<LocalDb_QingJingTable> lists = new ArrayList<>();
    private QingJingAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_wangguan);
        init();
    }

    private void init() {
        setView();
        onRefresh();
    }

    private void setView() {
        recyclerView = (EmptyRecycleView) findViewById(R.id.recyclerView);
        refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refreshLayout);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new QingJingAdapter(lists, this);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(this);
        adapter.setListeners(new QingJingAdapter.OnAdapterItemClickListener() {
            @Override
            public void click(LocalDb_QingJingTable bean, int position) {
                Intent intent = new Intent();
                intent.putExtra("name", bean.getName());
                intent.putExtra("id", bean.getId());
                setResult(ConstantConfigure.RESULT_SUCCESS, intent);
                finish();
            }
        });
        adapter.setlongListeners(new QingJingAdapter.OnAdapterItemLongClickListener() {
            @Override
            public void click(LocalDb_QingJingTable bean, int position) {
            }
        });
    }

    @Override
    public void onRefresh() {
        setRefreshing(true);
        lists.clear();
        lists.addAll(DataSupport.findAll(LocalDb_QingJingTable.class));
        adapter.notifyDataSetChanged();
        setRefreshing(false);
    }


    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }
}
